﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using GauntletFramework.Input;
using GauntletFramework.Games;
using GauntletFramework.StateManagment;
using GauntletFramework.StateManagment.Menu;


namespace GauntletFramework.StateManagment.Menu
{
    public class MenuState : State 
    {
        public override bool StartTransition { get { return false; } }
        public override bool EndTransition { get { return false; } }

        public override PhoneInputState DefualtPhoneInputState { get { return PhoneInputState.PhoneControlBottom; } }

        public static SpriteFont MenuFont = null;
        protected List<MenuItem> MenuItems = new List<MenuItem>();
        protected int SelectedItem;
        protected Texture2D Background;

        public MenuState()
        {
            if (MenuFont == null)
                MenuFont = Game.Content.Load<SpriteFont>(@"GauntletBase\MenuFont");
            
            SelectedItem = 0;
            MenuItems = new List<MenuItem>();
            Background = GauntletFramework.Texture.Gradiant.HorizontalGradiant(Game.GraphicsDevice, Color.Black, Color.Black);
        }

        public MenuState(List<MenuItem> MenuItems, Texture2D Background)
        {
            if (MenuFont == null)
                MenuFont = Game.Content.Load<SpriteFont>(@"GauntletBase\MenuFont");
            
            SelectedItem = 0;
            this.MenuItems = MenuItems;
            this.Background = Background;
        }

        public override void Update(GameTime gameTime)
        {
            if (MenuItems.Count == 0)
                return;
            
            BasicInput bi = ((GNTMainGame)Game).BasicInput;
            
            MenuItems[SelectedItem].selected = false;
            
            if (bi.ArrowDown == AdvancedButtonState.Pressed)
                SelectedItem++;
            if (bi.ArrowUp == AdvancedButtonState.Pressed)
                SelectedItem--;
            
            if (SelectedItem < 0)
                SelectedItem += MenuItems.Count;
            if (SelectedItem > MenuItems.Count-1)
                SelectedItem -= MenuItems.Count;

            MenuItems[SelectedItem].selected = true;
            
            if (bi.Action1 == AdvancedButtonState.Pressed || bi.ButtonYes == AdvancedButtonState.Pressed)
            {
                string target = MenuItems[SelectedItem].Target;
                if (target.ToLower().Contains("action "))
                {
                    if (target.ToLower() == "action back once")
                        this.Finished = true;
                    else if (target.ToLower() == "action back to main menu")
                        StaticGame.StateStack.BackToMenu();
                }
                else
                {
                    object obj = null;

                    try { obj = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(target); }
                    catch (Exception e) { Debug.Fail("Error in menu target (" + target + ")\n" + e.ToString()); }

                    if (obj != null)
                    {
                        if (obj is State)
                        {
                            State State = (State)obj;
                            StaticGame.StateStack.PushState(State);
                        }
                        else if (obj is GameStarter)
                        {
                            GameStarter starter = (GameStarter)obj;
                            StaticGame.StateStack.PushState(starter.CreateGame());
                        }
                        else
                        {
                            Debug.Fail("Error unknown type of menu target (" + target + ")");
                        }
                    }
                    else
                    {
                        Debug.Fail("Error no menu target (" + target + ")");
                    }

                }
            }

            foreach (MenuItem item in MenuItems)
                item.Update(gameTime);

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            
            SpriteBatch sb = StaticGame.SpriteBatch;
            sb.Draw(Background, Game.GraphicsDevice.PresentationParameters.Bounds, Color.White);
            
            foreach (MenuItem item in MenuItems)
                item.Draw(gameTime);
        }
    }
}
